package com.cykj.faceledtv.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.cykj.faceledtv.adapter.decoration.TopSmoothScroller;

import java.lang.ref.WeakReference;

/**
 * 无限循环recyclerView
 *
 * @author jzh
 * @date 2019/05/15 15:09
 */
public class LoopRecyclerView extends RecyclerView {
    private static final long TIME_AUTO_POLL = 1500;
    private AutoPollTask autoPollTask;
    private boolean running; //表示是否正在自动轮询
    private boolean canRun;//表示是否可以自动轮询
    private int dataCount = 0;
    private int position;
    private LinearLayoutManager manager;
    private TopSmoothScroller scroller;

    public LoopRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        scroller = new TopSmoothScroller(context);
        manager = new LinearLayoutManager(context);
        setLayoutManager(manager);
        autoPollTask = new AutoPollTask(this);
    }

    private static class AutoPollTask implements Runnable {
        private final WeakReference<LoopRecyclerView> mReference;

        //使用弱引用持有外部类引用->防止内存泄漏
        AutoPollTask(LoopRecyclerView reference) {
            this.mReference = new WeakReference<>(reference);
        }

        @Override
        public void run() {
            LoopRecyclerView recyclerView = mReference.get();
            if (recyclerView != null && recyclerView.running && recyclerView.canRun) {
                if (recyclerView.dataCount > 0) {
                    if (recyclerView.position != 0 &&
                            recyclerView.manager.findLastVisibleItemPosition() == recyclerView.dataCount - 2) {
                        recyclerView.position = 0;
                        recyclerView.postDelayed(recyclerView.autoPollTask, TIME_AUTO_POLL * 2);
                        return;
                    }
                    recyclerView.position++;
                    recyclerView.scroller.setTargetPosition(recyclerView.position);
                    recyclerView.manager.startSmoothScroll(recyclerView.scroller);
                }
                recyclerView.postDelayed(recyclerView.autoPollTask, TIME_AUTO_POLL);
            }
        }
    }

    public void setDataCount(int dataCount) {
        this.dataCount = dataCount;
        position = 0;
    }

    //开启:如果正在运行,先停止->再开启
    public void start() {
        if (running)
            stop();
        canRun = true;
        running = true;
        postDelayed(autoPollTask, TIME_AUTO_POLL);
    }

    public void stop() {
        running = false;
        removeCallbacks(autoPollTask);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                position = manager.findFirstVisibleItemPosition();
                if (running) {
                    stop();
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
                if (canRun)
                    start();
                break;
        }
        return super.onTouchEvent(e);
    }
}
